<?php
// +----------------------------------------------------------------------
// | Tpp
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.intaotao.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: KuangGanLin < 529429490@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\facade\Cookie;
use \think\facade\Session;
use app\admin\controller\Permissions;

use app\admin\logic\CouponLogic;
use app\common\model\Coupon as couponModel;
use app\common\model\User;
use app\common\utils\UniqueUtil;


class Coupon extends Permissions
{
    public function index()
    {
        $model = new couponModel();
        $post = $this->request->param();
        
		
        if (isset($post['curr_id']) and $post['curr_id']!= '') {
             //$where['curr_id'] = $post['curr_id'];
             $where[] = ['curr_id', '=', $post['curr_id']];//tp5.1
             $curr_id = $post['curr_id'];	
        }else{
            $curr_id = '';
        }
        $coupon_list = empty($where) ? $model->where('coup_status','neq',400)->order('create_time asc')->paginate(20,false,['type'=>'page\Page','var_page'=>'page']) : $model->where('coup_status','neq',400)->where($where)->order('create_time asc')->paginate(20,false,['query'=>$this->request->param(),'type'=>'page\Page','var_page'=>'page']);
        foreach($coupon_list as $key => &$val){
            $curr_info = Db::name('curriculum')->where(array('curr_id'=>$val['curr_id']))->field('curr_name')->find();
            $val['curr_name'] = $curr_info['curr_name'];
            
            if($val['coup_type'] == 0){
                $val['coup_type_val'] ='课程授权码优惠卷';
            }else{
                $val['coup_type_val'] ='课程授权码优惠卷';
            }
        }
        //取得课程信息
        $curriculum_list = Db::name('curriculum')->where(array('curr_status'=>2))->field('curr_id,curr_name')->select();
        $this->assign('curriculum_list',$curriculum_list);
        
        $this->assign('curr_id',$curr_id);		
        $this->assign('coupon_list',$coupon_list);
        return $this->fetch();
    }




    public function publish()
    {
        $logic = new CouponLogic();	
    	//获取菜单id
    	$coup_id = $this->request->has('coup_id') ? $this->request->param('coup_id', 0, 'intval') : 0;
    	$model = new couponModel();
        $userModel = new User();
        //取得课程信息
        $curriculum_list = Db::name('curriculum')->where(array('curr_status'=>2))->field('curr_id,curr_name')->select();
        $this->assign('curriculum_list',$curriculum_list);
        //是正常添加操作
        if($coup_id > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate;
                $validate->rule([
                    //'coup_code|优惠卷使用码'  => 'require',
                    'coup_price|抵扣金额'  => 'require',
                    'validity_time|有效期'  => 'require',
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                if($post['use_count'] > $post['coup_count']){
                    return $this->error('使用数量不能大于优惠券总数量');
                }
                
                //验证菜单是否存在
                $coupon_info = $model->where('coup_id',$coup_id)->find();
                if(empty($coupon_info)) {
                    return $this->error('coup_id不正确');
                }
                //更新信息
                $res = $logic->updateCoupon($coup_id,$post);
                if($res['status'] == 1){
                    return $this->success('修改成功',$post['prevurl']);   
                }else{
                    return $this->error($res['msg']);
                }
            } else {
                //非提交操作
                $coupon_info = $model->where('coup_id',$coup_id)->find();
                
                if(!empty($coupon_info)) {
                    $this->assign('coupon_info',$coupon_info);
                    return $this->fetch();
                } else {
                        return $this->error('coup_id不正确');
                }
            }
    	} else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //$post['update_time']=time();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate;
                $validate->rule([
                    //'coup_code|优惠卷使用码'  => 'require',
                    'coup_price|抵扣金额'  => 'require',
                    'validity_time|有效期'  => 'require',
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }	
                if($post['use_count'] > $post['coup_count']){
                    return $this->error('使用数量不能大于优惠券总数量');
                }
                
                //新增信息
                $res = $logic->addCoupon($post);
                if($res['status'] == 1){
                    return $this->success('添加成功',$post['prevurl']);   
                }else{
                    return $this->error($res['msg']);
                }	
            } else {
                
                //初始化值
                $coupon_info = array(
                    'curr_id' =>0,
                    'coup_id' =>0,
                    'coup_type' =>0,
                    'coup_status'=>0,
                    'use_count'=>0,
                    'create_time' => date('Y-m-d H:i:s')
                );
                $auth_user_list = array();
                $this->assign('coupon_info',$coupon_info);
                return $this->fetch();
            }
    	}
    }

	



    public function delete()
    {
    	if($this->request->isAjax()) {
            $coup_id = $this->request->has('coup_id') ? $this->request->param('coup_id', 0, 'intval') : 0;
            //if(false == Db::name('curriculum')->where('coup_id',$coup_id)->delete()) {
            if(false == Db::name('coupon')->where('coup_id',$coup_id)->update(['coup_status'=>400])) {	
                return $this->error('删除失败');
            } else {
                return $this->success('删除成功','admin/coupon/index');
            }
    	}
    }


    public function is_top()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            if(false == Db::name('article')->where('id',$post['id'])->update(['is_top'=>$post['is_top']])) {
                return $this->error('设置失败');
            } else {
                return $this->success('设置成功','admin/coupon/index');
            }
        }
    }


    public function status()
    {
        if($this->request->isPost()){
            $post = $this->request->post();	
            if(false == Db::name('coupon')->where('coup_id',$post['id'])->update(['coup_status'=>$post['status']])) {
                return $this->error('设置失败');
            } else {
                return $this->success('设置成功','admin/coupon/index');
            }
        }
    }
		
}
